<?php
    class Task extends CActiveRecord{
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        // dat luat cho model
        public function rules(){}         

        // goi den bang can ket noi   
        public function tableName() {                  
            return 'pm_task';
        }

        // nhan cac thuong tinh 
        public function attributeLabels(){}

        public function getRowByAlias($alias){
            //$id = intval($id);
            // $cache = false;
            $row = array();            
            $sql = "SELECT * FROM pm_task WHERE project_alias='".$alias."'";           
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();                
            return $row;
        }    

        public function getRowById($id){
            $id = intval($id);
            // $cache = false;
            $row = array();
            $sql = "SELECT pm_task.*,pm_user.fullname,pm_version.version_name,pm_category.category_name FROM pm_task INNER JOIN pm_user ON pm_task.task_create_user = pm_user.user_id INNER JOIN pm_version ON pm_task.task_version = pm_version.version_id INNER JOIN pm_category ON pm_task.task_category = pm_category.category_id WHERE task_id=".$id;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();

            return $row;
        }

        public function getRowByProjectId($project_id,$status_id){
            $sql_plus = "";
            if($status_id != 0){
                $sql_plus = " AND pm_task.task_status=".$status_id;
            }
            $sql = "SELECT pm_task.*,pm_category.category_name,pm_assign.assign_user_id,pm_user.fullname FROM pm_task INNER JOIN pm_assign ON pm_task.task_id = pm_assign.task_id INNER JOIN pm_category ON pm_task.task_category = pm_category.category_id INNER JOIN pm_user ON pm_assign.assign_user_id = pm_user.user_id WHERE pm_task.project_id=".$project_id . $sql_plus . " GROUP BY task_id";          
            //echo $sql;die;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();            
            return $row;
        }
        
        public function getRowByUserId($user_id){            
            $sql = "SELECT pm_task.*,pm_category.category_name,pm_assign.assign_user_id,pm_user.fullname FROM pm_task INNER JOIN pm_assign ON pm_task.task_id = pm_assign.task_id INNER JOIN pm_category ON pm_task.task_category = pm_category.category_id INNER JOIN pm_user ON pm_assign.assign_user_id = pm_user.user_id WHERE pm_assign.assign_user_id=".$user_id;          
            //echo $sql;die;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();            
            return $row;
        }

        public function getLatestRows($array_input,$limit){        
            $row = array();

            $sql_where='';
            foreach($array_input as $key=>$value)
            {
                $sql_where.= " AND " . $key."='".$value."',"; 
            }
            $sql_where = substr_replace($sql_where ,"",-1);
            $sql = "SELECT * FROM pm_task WHERE 1=1".$sql_where . " ORDER BY `project_create_date` desc" . " LIMIT 0,".$limit;                              
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();

            return $row;
        }

        public function getAllRows($closed){
            $rows = array();
            $cache = Yii::app()->cache;
            if($cache != null){
                $cacheService = new CacheService("user","getAllRows","");
                $key = $cacheService->createKey();
                $dependency = $cacheService->createDependency();
                $cache = Yii::app ()->cache->get ( $key ); 
            }
            $cache=false;
            if($cache == false){ 
                $sql_add = "";
                if($closed == 0){
                    $sql_add = " AND project_status = 1";
                }
                $sql = "SELECT * FROM pm_task WHERE project_public = 1".$sql_add;        
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $rows = $command->queryAll();

                Yii::app ()->cache->set ( $key, $rows, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
            }else{
                $rows = $cache;
            }
            return $rows;
        }    

        public function insertObject($array_input){
            $sql='';
            foreach($array_input as $key=>$value)
            {
                $sql.=$key."='".$value."',"; 
            }
            $sql='INSERT INTO pm_task SET '.$sql;
            $sql=rtrim($sql,',');
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $command->execute();
            $record_id=Yii::app()->db->getLastInsertID();  
            return $record_id;
        }

        public function updateObject($array_input,$key_id,$key_value){
            $sql='';
            foreach($array_input as $key=>$value)
            {
                $sql.=$key."='".$value."',"; 
            }
            $sql=rtrim($sql,',');
            if($sql!='')
            {
                $sql='UPDATE pm_task SET '.$sql.' WHERE '.$key_id.'='.$key_value;
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $a=$command->execute();
                return $a;
            }
            return -1;
        }    

        public function deleteObject($array_input){
            $sql=' 1 ';
            foreach($array_input as $key=>$value)
            {
                $sql.=" AND ".$key."='".$value."'"; 
            }
            $sql=rtrim($sql,',');
            if($sql!='')
            {
                $sql='delete from pm_task where '.$sql.'';
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $a=$command->execute();
                if($a) return 1;
                else return 0;
            }
            else return 0;

        }

    }

?>
